package com.soft.controller;

import com.soft.pojo.PageBean;
import com.soft.pojo.Salary;
import com.soft.service.SalaryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruoxi
 * @create 2021-12-27 11:20
 */
@WebServlet("/showSalaryInfoByPage")
public class ShowSalaryInfoByPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SalaryService salaryService = new SalaryService();
        PageBean<Salary> salaryInfoPg = new PageBean<>();

        int currentPage = 1;
        String currentPage_ = req.getParameter("currentPage");
        if (currentPage_ != null) {
            currentPage = Integer.parseInt(currentPage_);
        }

        int currentCount = 10;

        salaryInfoPg.setCurrentPage(currentPage);
        salaryInfoPg.setCurrentCount(currentCount);
        salaryInfoPg.setTotalCount(salaryService.findSalaryCount());
        salaryInfoPg.setTotalPage((int) Math.ceil(salaryService.findSalaryCount() * 1.0 / currentCount));
        salaryInfoPg.setList(salaryService.findSalaryByPage((currentPage - 1) * currentCount, currentCount));

        req.setAttribute("salaryInfoPg", salaryInfoPg);
        req.getRequestDispatcher("salaryInfo.jsp").forward(req, resp);
    }
}
